package net.flyinggroup.smb.service;

import jcifs.config.PropertyConfiguration;
import jcifs.context.BaseContext;
import jcifs.smb.SmbFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Properties;

@Service
public class SmbService {
    @Value("${jcifs.smb.client.domain:WORKGROUP}")
    private String domain;
    @Value("${jcifs.smb.client.username}")
    private String username;
    @Value("${jcifs.smb.client.password}")
    private String password;

    public void printFile() {
        SmbFile smbFile = null;
        Properties properties = new Properties();
        properties.setProperty("jcifs.smb.client.domain", domain);
        properties.setProperty("jcifs.smb.client.username", username);
        properties.setProperty("jcifs.smb.client.password", password);
        try {
            BaseContext context = new BaseContext(new PropertyConfiguration(properties));
            smbFile = new SmbFile("smb://192.168.28.18/work/data.json", context);
            System.out.write(smbFile.getInputStream().readAllBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (Objects.nonNull(smbFile)) {
                smbFile.close();
            }
        }
    }

    public String getPasswordUsingEnvironment(Environment environment) {
        return environment.getProperty("jcifs.smb.client.username");
    }
}
